package com.dhcc.DRGsHosp.facade.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;

import com.dhcc.DRGsHosp.application.SumExpenseApplication;
import com.dhcc.DRGsHosp.facade.SumExpenseFacade;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;

/**
 * 
 * @ClassName: SumExpenseFacadeImpl  
 * @Description ：  费用中间表定时计算  
 * @Date: 2017年9月19日 下午5:22:07  
 * @Author LiuHongJun 
 * @Version 3.0.0 DHC-DRGs
 */
@Named
public class SumExpenseFacadeImpl implements SumExpenseFacade {
	@Autowired
	private  SumExpenseApplication sumExpenseApplication;
	
	/**
	 * 	@Discription：定时计算费用中间表数据,定时任务调用转接，以QuartzJob结尾
	 *  @Author QiaoYiLong  
	 *  @Date: 2018年10月26日 9:18:07  
	 *  @param 默认格式为 0_0_pushDate
	 *  cron 表达式 0 0 5 * * ? 每天凌晨5:00计算前一天的费用中间表
	 */
	public void sumExpenseQuartzJob(String param) {
		if(null != param && !"".equals(param) && param.split("_").length >= 3){
			String datas[] = param.split("_");
			String dateFrom = datas[0];
			String dateTo = datas[1];
			String dateType = datas[2];
			sumExpense(dateFrom, dateTo, dateType);		
		}
	}
	/**
	 * 
	 *  @Description： 费用中间表定时计算  
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.SumExpenseFacade#sumExpense()
	 */
	@Override
	public void sumExpense(String dateFrom,String dateTo,String dateType) {
		//首先判断是不是手动触发，若dateFrom，dateTo都不为0则为手动
		if(!"0".equals(dateTo) && !"0".equals(dateFrom)) {
			dateFrom = DrgsIndex.formateDateFrom(dateFrom) ;
			dateTo = DrgsIndex.formateDateTo(dateTo) ;
			List<Map<String,Object>> list=new ArrayList<>();
			if("outDate".equals(dateType)){
				list=sumExpenseApplication.findMrParidByOut( dateFrom, dateTo);
			}else{
				list=sumExpenseApplication.findMrParid( dateFrom, dateTo);	
			}
			if(!list.isEmpty()) {
				for(Map<String, Object> map : list) {
					String id=map.get("ID")==null?"":map.get("ID").toString();
					System.out.println("正在计算"+id);
					sumExpenseApplication.sumExpense(id);
				}
			}else {
				System.out.println("无数据！");
			}
			System.out.println("计算完毕！");
			return;
		}else{
			//定时任务触发
			dateFrom = DrgsIndex.formateDateFrom(dateFrom) ;
			dateTo = DrgsIndex.formateDateTo(dateTo) ;
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
	        Date date=new Date();  
	        Calendar calendar = Calendar.getInstance();  
	        calendar.setTime(date);  
	        calendar.add(Calendar.DAY_OF_MONTH, -1);  
	        date = calendar.getTime();  
			dateFrom=sdf.format(date);
			dateTo=sdf.format(date);
			List<Map<String,Object>> list=new ArrayList<>();

	        //定时任务使用java反射时必须重新实例化该接口，bean括号里面填impl实例上面的@Name括号里面的内容
	        if(sumExpenseApplication==null){
	        	sumExpenseApplication =(SumExpenseApplication) ContextLoader.getCurrentWebApplicationContext().getBean("sumExpenseApplicationImplqiaoyilong20181026");
	        }
			if("outDate".equals(dateType)){
				list=sumExpenseApplication.findMrParidByOut( dateFrom, dateTo);
			}else{
				list=sumExpenseApplication.findMrParid( dateFrom, dateTo);	
			}
			if(!list.isEmpty()) {
				for(Map<String, Object> map : list) {
					String id=map.get("ID")==null?"":map.get("ID").toString();
					System.out.println("正在计算"+id);
					sumExpenseApplication.sumExpense(id);
				}
			}else {
				System.out.println("无数据！");
			}
			System.out.println("计算完毕！后续会根据推送日期每天定时更新！");			
		}
	}
/**
 * 
 *  @return 
 * @Description：删除特殊字符（科室，医师，病区编码里面的）
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.SumExpenseFacade#replaceCodeStr()
 */
@Override
public Object handelSpecCode() {
	try {
		sumExpenseApplication.handelSpecCode();
	} catch (Exception e) {
		e.printStackTrace();
	}
//	sumExpenseApplication
	return null;
}

}
